
clear
set more off
capture log close
set seed 12345
set matsize 10000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"


*************
**  Prep entrants and separaters at firm-level
*************
use $DATA/scorp100p_entsep_file, clear

collapse (count) tin (sum) ft stayy_ft enty_ft sepy_ft stay12_ft ent12_ft wages_* wages_tot=wages wages_totft=wagesft , by(firm_tin year)
ren tin n_ee_entsep
sort firm_tin year
tempfile tt
save `tt', replace

use $DATA/reg_file_firm.dta, clear
sort firm_tin year
merge 1:1 firm_tin year using `tt'
drop _merge
drop n_ee 
ren n_ee_entsep n_ee
ren ft n_ft

gen z12 = .
replace z12 = n_ft if year==2012
bys firm_tin: egen n_ft_2012 = max(z12)
drop z12

* rates 
sort firm_tin year
by firm_tin: gen dn_eey_ft = (n_ft-n_ft[_n-1])/n_ft[_n-1] 
by firm_tin: gen dn_enty_ft = enty_ft/n_ft[_n-1] 
by firm_tin: gen dn_stayy_ft = stayy_ft/n_ft[_n-1] 

* relative to 2012
gen dn_ft12 = (n_ft-n_ft_2012)/n_ft_2012
gen dn_stay12_ft = (stay12_ft)/n_ft_2012
gen dn_ent12_ft = (ent12_ft)/n_ft_2012

foreach n in n_ft stayy_ft enty_ft ent12_ft stay12_ft {
	gen ln`n' = ln(`n')
}


*************************
*************************
*************************
**  Figure 3 - extensive margin employment results 
*************************
*************************
*************************

local bm1 = `baseyr'-1
local bp1 = `baseyr'+1

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
* base 2011,2012
local events "t`yrb'-t2010 t`bp1'-t`yre' y`yrb'-y2010 y`bp1'-y`yre' `t'"

*************************
**  decompostion entrance separations [Panel B]
*************************
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.fsize_`baseyr'##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"

foreach r in 1 {
	eststo r`r'_ee: quietly regress n_ft `events' `r`r'' , vce(cluster firm_tin)
	eststo r`r'_ent: quietly regress enty_ft `events' `r`r'' , vce(cluster firm_tin)
	eststo r`r'_sep: quietly regress stayy_ft `events' `r`r'' , vce(cluster firm_tin)
}
esttab using "$OUTPUT/figure3_panelB.csv", replace se ar2 mtitles( ) keep(t20*) 
eststo clear


*************************
**  log employment [Panel A]
*************************
foreach p in n_ft_lower {
	gen ln`p'=ln(`p') 
}
local r1 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"

foreach o in n_ft_lower {
	* loop over regressions
	foreach r in 1 {
		eststo r`r': quietly reghdfe `rlog'`o' `events'  [aw=`o'_2012] if (`t'==1 | `t'==0), absorb(`r`r'' firm_tin) vce(cluster firm_tin)
	}
	esttab using "$OUTPUT/figure3_panelA.csv", replace se ar2 mtitles( ) keep(t20*) addnote("Each specification includes a different set of control variabels")
	eststo clear
}


***********************
***********************
***********************
**  Table 3 - employment decomp 
***********************
***********************
***********************
local r1 "i.valaddpw_ft_2012cat##ib`baseyr'.year i.fsize_ft_2012##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.state_`baseyr'##ib`baseyr'.year"

* loop over regressions
foreach r in 1 {
	eststo r`r'_ee: quietly regress dn_ft12 `events' `r`r''  [aw=n_ft_2012] if year==2016, vce(cluster firm_tin)
	eststo r`r'_ent: quietly regress dn_ent12_ft `events' `r`r''  [aw=n_ft_2012] if year==2016, vce(cluster firm_tin)
	eststo r`r'_sep: quietly regress dn_stay12_ft `events' `r`r''  [aw=n_ft_2012] if year==2016, vce(cluster firm_tin)
}
esttab using "$OUTPUT/table3_employment.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear




clear
exit
